<template>
  <div>
    <a-tabs @change="listBy">
      <a-tab-pane tab="班组设置"  key="1"  forceRender>
        <SsPersonGroupListG></SsPersonGroupListG>

      </a-tab-pane>
      <a-tab-pane tab="人员班组信息"  key="2">
        <SsPersonGroupListP></SsPersonGroupListP>

      </a-tab-pane>
    </a-tabs>
  </div>
</template>

<script lang="ts" name="baseinfo-ssPersonGroup" setup>
  import {ref, reactive, computed, unref} from 'vue';
  import {BasicTable, useTable, TableAction} from '/@/components/Table';
  import {useModal} from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage'
  import SsPersonGroupModal from './components/SsPersonGroupModal.vue'
  import SsPersonGroupQueryModal from './components/SsPersonGroupQueryModal.vue'
  import SsPersonGroupAddModal from './components/SsPersonGroupAddModal.vue'
  import SsPersonGroupListP from "./SsPersonGroupListP.vue";
  import SsPersonGroupListG from "./SsPersonGroupListG.vue";
  import {columns, searchFormSchema, superQuerySchema} from './SsPersonGroup.data';
  import {list, deleteOne, batchDelete, getImportUrl,getExportUrl} from './SsPersonGroup.api';
  import { downloadFile } from '/@/utils/common/renderUtils';
  import { useUserStore } from '/@/store/modules/user';
  import {useMessage} from "@/hooks/web/useMessage";
  const queryParam = reactive<any>({});
  const checkedKeys = ref<Array<string | number>>([]);
  const userStore = useUserStore();
  //注册model
  const [registerModal, {openModal}] = useModal();
  const { createMessage, createErrorModal,createWarningModal } = useMessage();
  const [registerModalQuery, {openModal: openModelQuery}] = useModal();
  const [registerModalAdd, {openModal: openModelAdd}] = useModal();

  //注册table数据
  const { prefixCls,tableContext,onExportXls,onImportXls } = useListPage({
    tableProps:{
      title: '人员分组',
      api: list,
      columns,
      canResize:false,
      formConfig: {
        //labelWidth: 120,
        schemas: searchFormSchema,
        autoSubmitOnEnter:true,
        showAdvancedButton:true,
        fieldMapToNumber: [
        ],
        fieldMapToTime: [
        ],
      },
      actionColumn: {
        width: 120,
        fixed:'right'
      },
      beforeFetch: (params) => {
        return Object.assign(params, queryParam);
      },
    },
    exportConfig: {
      name:"人员分组",
      url: getExportUrl,
      params: queryParam,
    },
    importConfig: {
      url: getImportUrl,
      success: handleSuccess
    },
  })

  const [registerTable, {reload},{ rowSelection, selectedRowKeys }] = tableContext

  // 高级查询配置
  const superQueryConfig = reactive(superQuerySchema);

  /**
   * 高级查询事件
   */
  function handleSuperQuery(params) {
    Object.keys(params).map((k) => {
      queryParam[k] = params[k];
    });
    reload();
  }
  /**
   * 新增事件
   */
  function handleAdd() {
    openModal(true, {
      isUpdate: false,
      showFooter: true,
    });
  }
  /**
   * 编辑事件
   */
  function handleEdit(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
      showFooter: true,
    });
  }
  /**
   * 详情
   */
  function handleDetail(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
      showFooter: false,
    });
  }


  function handleAddPerson(record: Recordable) {
    const select = rowSelection.selectedRows;
    if(select.length != 1){
      return createMessage.warn("请选择一个分组查看操作")
    }
    console.log(select)
    openModelAdd(true, {
      select,
      isUpdate: true,
      showFooter: false,
    });
  }


  function handleQueryPerson(record: Recordable) {
    const select = rowSelection.selectedRows;
    if(select.length != 1){
      return createMessage.warn("请选择一个分组操作")
    }
    openModelQuery(true, {
      select,
      isUpdate: true,
      showFooter: false,
    });
  }

  function handleQueryPerson1(record: Recordable) {
    const select = rowSelection.selectedRows;
    if(select.length != 1){
      return createMessage.warn("请选择一个分组查看")
    }
    openModelQuery(true, {
      select,
      isUpdate: true,
      showFooter: false,
      view:true,
    });
  }


  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteOne({id: record.id}, handleSuccess);
  }
  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDelete({ids: selectedRowKeys.value}, handleSuccess);
  }
  /**
   * 成功回调
   */
  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }
  /**
   * 操作栏
   */
  function getTableAction(record){
    return [
      {
        label: '编辑',
        onClick: handleEdit.bind(null, record),
      }
    ]
  }
  /**
   * 下拉操作栏
   */
  function getDropDownAction(record){
    return [
      {
        label: '详情',
        onClick: handleDetail.bind(null, record),
      }, {
        label: '删除',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
          placement: 'topLeft',
        }
      }
    ]
  }


</script>

<style scoped>

</style>
